package com.umeox.watch.moto.chat.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.umeox.moto.common.log.Logger
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.bean.EmojiBean

/**
 * Created by Rambo 2021/8/23
 * Describe:
 */
class EmojiAdapter(private val emojiBeans: List<EmojiBean>) :
    RecyclerView.Adapter<EmojiAdapter.EmojiHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojiHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.item_emoji, parent, false)
        return EmojiHolder(view)
    }

    override fun onBindViewHolder(holder: EmojiHolder, position: Int) {

        val emojiBean: EmojiBean = emojiBeans[holder.adapterPosition]
        holder.mEmojiIv.setImageResource(emojiBean.resourceId)
        holder.itemView.setOnClickListener {
            if (mOnItemClickListener != null) {
                mOnItemClickListener!!.invoke(emojiBean.emojiId)
            }
        }
    }

    override fun getItemCount(): Int {
        return emojiBeans.size
    }

    private var mOnItemClickListener: ((id: Int) -> Unit)? = null

    fun setOnItemClickListener(onItemClickListener: ((id: Int) -> Unit)) {
        mOnItemClickListener = onItemClickListener
    }


    class EmojiHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mEmojiIv: ImageView = itemView.findViewById(R.id.ivEmoticon)
    }
}